<!DOCTYPE html>

<html>

<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    table {
      border-collapse: collapse;
      border-spacing: 0;
      border: 1px solid #c0c0c0;
      width: 500px;
      margin: 100px auto;
      text-align: center;
    }

    th {
      background-color: #09c;
      font: bold 16px "微软雅黑";
      color: #fff;
      height: 24px;
    }

    td {
      border: 1px solid #d0d0d0;
      color: #404060;
      padding: 10px;
    }

    .allCheck {
      width: 80px;
    }
  </style>
</head>

<body>
  <table>
    <tr>
      <th class="allCheck">
        <input type="checkbox" name="" id="checkAll"> <span class="all">全选</span>
      </th>
      <th>商品</th>
      <th>商家</th>
      <th>价格</th>
    </tr>
    <tr>
      <td>
        <input type="checkbox" name="check" class="ck">
      </td>
      <td>小米手机</td>
      <td>小米</td>
      <td>￥1999</td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" name="check" class="ck">
      </td>
      <td>小米净水器</td>
      <td>小米</td>
      <td>￥4999</td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" name="check" class="ck">
      </td>
      <td>小米电视</td>
      <td>小米</td>
      <td>￥5999</td>
    </tr>
  </table>
  <script>
    // 思路分析：
    //  1. 获取元素（一个#checkAll   3个.ck）
    //  2. 给checkAll 注册click，点击的时候，让下面三个ck的选中状态和全选的选中状态一致
    //       2.1 先获取到全选的选中状态 checked 属性
    //       2.2 让下面三个ck的选中状态（ checked 属性）和全选一致

    const checkAll = document.querySelector('#checkAll')
    const cks = [...document.querySelectorAll('.ck')]

    checkAll.addEventListener('click', () => cks.forEach(item => item.checked = checkAll.checked))
      // console.log(this.checked) // 全选的选中状态
      // for (let i = 0; i < cks.length; i++) {
      //   cks[i].checked = this.checked
      // }

      
      // const ck = cks.map(item => item.checked = this.checked)
      // cks.innerHTML = ck
      
    


    // for (let i = 0; i < cks.length; i++) {
    //   cks[i].addEventListener('click', function () {
    //     // checkAll.checked = document.querySelectorAll('.ck:checked').length === 3
    //     checkAll.checked = document.querySelectorAll('.ck:checked').length === cks.length
    //   })
    // }

    cks.forEach(item => item.addEventListener('click' , () => checkAll.checked = cks.every(item => item.checked === true)))
  </script>
</body>

</html>